package org.itboys.mobile.controller.site;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.JoinMemberEducation;
import org.itboys.mobile.service.mongo.site.JoinSiteMemberEducationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/9/29
 * 描述：雄安加盟网_
 */
@Controller
@RequestMapping("/join/site/education")
public class JoinSiteEducationController extends BaseController{
    @Autowired
    private JoinSiteMemberEducationService educationService;

    /**
     * 新增
     * @param request
     * @param response
     * @param memberId
     * @param school
     * @param className
     * @param startTime
     */
    @RequestMapping("/save")
    public void save(HttpServletRequest request,
                     HttpServletResponse response,
                     @RequestParam(value="memberId",required=true)Long memberId,
                     @RequestParam(value="school",required=true)String school,
                     @RequestParam(value="className",required=true)String className,
                     @RequestParam(value="startTime",required=true)String startTime
    ){
        try {
            educationService.save(new JoinMemberEducation(memberId,school,className,startTime));
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));

        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/join/site/education/save------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));
        }

    }

    /**
     * 查询
     * @param request
     * @param response
     * @param memberId
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="memberId",required=true)Long memberId
    ) {
        try {
            Map<String,Object> param=new HashMap<String,Object>();
            param.put("isDeleted", SundayCommonConstants.NO);
            param.put("memberId",memberId);
            List<JoinMemberEducation> result=educationService.list(param);
            AjaxUtils.renderJson(response,result);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    /**
     * 删除
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/delete")
    public void delete(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="id",required=true) Long id

    ) {
        try {
            educationService.delete(id);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {

            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/join/site/education/delete------" + e.getMessage());
            }
            e.printStackTrace();

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
}
